package com.example.simplecardbasic.message;

import com.aliyuncs.exceptions.ClientException;
import com.example.simplecardbasic.Utils.SmsUtil;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 接受mq消息
 */
@Component
public class MqReceiver {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //@RabbitListener(queues = "myQueue")
    //自动创建队列
    //@RabbitListener(queuesToDeclare = @Queue("myQueue"))
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fruitQueue"),
            key = "fruit",
            exchange = @Exchange("myOrder")
    ))
    public void processFruit(String message) {
        System.out.println("fruit message:" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("computerOrder"),
            key = "computer",
            exchange = @Exchange("myOrder")
    ))
    public void processComputer(String message) {
        System.out.println("computer message:" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("sendSms"),
            exchange = @Exchange("sms"),
            key = "phone"
    ))
    public void processSms(String phone) throws ClientException{
        SmsUtil.sendSms(phone, stringRedisTemplate);
    }
}
